package com.freshcity.page.friend
{
	import com.freshcity.component.FLabel;
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.utils.MessageUtil;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;

	public class RequestFriend extends Component
	{
		private var backMask:Sprite;
		private var back:RequestFriendWin;
		private var noflowerTip:FLabel;
		private var userId:String;

		public function RequestFriend(userId:*)
		{
			this.userId=userId;
			super(LayerManager.alertLayer, 0, 0);
			back.closeBtn.addEventListener(MouseEvent.CLICK, hide);
			back.cancelBtn.addEventListener(MouseEvent.CLICK, hide);
			back.submitBtn.addEventListener(MouseEvent.CLICK, submitBtnHandler);
			back.flowerAmt.addEventListener(Event.CHANGE, flowerAmtHandler);
		}

		protected function submitBtnHandler(event:MouseEvent):void
		{
			var i:int=int(back.flowerAmt.selectedLabel);
			if (back.flowerAmt.selectedIndex <= 0)
			{
				main.msg.showRollTipsMsg("请选择想送的玫瑰数量");
			}
			else if (i > main.user.flowerAmt)
			{
				if(main.user.sex=='male'){
					var s2:String="".concat("玫瑰数量不足。赠人清水可获得玫瑰奖励。<br/>", "<p align='right'>&nbsp;", "<a href='event:'>", ">>立刻前往", "</a></p>");
					main.msg.showPopUpMsg(s2, MsgManager.CRY, 0, MsgManager.CENTER, forwardPresendWater);
				}else{
					main.msg.showRollTipsMsg("玫瑰数量不足。保持每天登录可累积玫瑰。");
				}
			}
			else
			{
				main.commu.send(2308, {i: i, to: userId}, onReqFriendCallback);
			}
		}

		private function forwardPresendWater(event:Event=null):void
		{
			this.hide();
			main.mainNav.onShowTree();
		}

		private function onReqFriendCallback(data:*):void
		{
			main.commu.unRegister(2308);

			main.msg.showRollTipsMsg("玫瑰已送出,期待好运吧。");
			main.friendPanel.addFriend(data);
			Dispatcher.dispatchEvent(new DataEvent(EventName.ReqFriendDone, data));
			this.hide();
		}

		protected function flowerAmtHandler(event:Event):void
		{
			if (back.flowerAmt.selectedIndex <= 0)
			{
				back.flowerDesc.text="";
			}
			else
			{
				var amt:String=back.flowerAmt.selectedLabel;
				var s:String="花语:" + MessageUtil.decodeFlowerMean(amt);
				back.flowerDesc.text=s;
			}
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new RequestFriendWin();
			this.addChild(back);
			back.flowerDesc.text="";
		}

		override public function destroy():void
		{
			back.closeBtn.removeEventListener(MouseEvent.CLICK, hide);
			back.cancelBtn.removeEventListener(MouseEvent.CLICK, hide);
			back.submitBtn.removeEventListener(MouseEvent.CLICK, submitBtnHandler);
			back.flowerAmt.removeEventListener(Event.CHANGE, flowerAmtHandler);

			super.destroy();
		}

		override public function hide(event:Event=null):void
		{
			// TODO Auto Generated method stub
			super.hide(event);
		}

		override public function show(event:Event=null):void
		{
			backMask=new Sprite();
			backMask.graphics.clear();
			backMask.graphics.beginFill(0xffffff, 0);
			backMask.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			backMask.graphics.endFill();
			this.addChildAt(backMask, 0);

			this.x=(stage.stageWidth - back.width) / 2;
			this.y=(stage.stageHeight - back.height) / 2;
			back.flowerAmt.selectedIndex=0;
			super.show(event);
		}

	}
}
